package fetcher

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"strings"
	"time"
)
var SID string = ""
var SIDchan chan string = make(chan string)

func GetSID(){
	for{
		SID = <- SIDchan
	}
}
var rateLimiter = time.Tick(3*time.Second)
func Fetch(keyword,pn string) []byte{
	<-rateLimiter
	webUrl :="https://www.lagou.com/jobs/list_/p-city_0?px=new"
	headersUrl := "https://www.lagou.com/jobs/positionAjax.json?px=new&needAddtionalResult=false"
	cookieJar, err := cookiejar.New(nil)
	if err != nil {
		panic(err)
	}
	client := http.Client{Jar: cookieJar }
	request, e := http.NewRequest("GET", webUrl, nil)
	if e!=nil{
		panic(e)
	}
	request.Header.Set("Accept","application/json, text/javascript, */*; q=0.01")
	request.Header.Set("Referer", webUrl)
	request.Header.Set("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
	response, e := client.Do(request)
	if e!=nil{
		panic(e)
	}
	form := url.Values{"first":{"false"},"pn":{pn},"kd":{keyword},"sid":{SID}}
	if pn == "1" {
		form.Set("first","true")
	}
	fmt.Printf("发送请求:参数为 %v \n",form.Encode())
	body := strings.NewReader(form.Encode())
	request, e = http.NewRequest("POST", headersUrl,body)
	request.Header.Set("Accept","application/json, text/javascript, */*; q=0.01")
	request.Header.Set("Referer", webUrl)
	request.Header.Set("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
	cookies := response.Cookies()
	for _,v := range cookies{
		//cookie := &http.Cookie{
		//	Name:    v.Name,
		//	Value:   v.Value,
		//	Path:    v.Path,
		//	Domain:  v.Domain,
		//	MaxAge:  v.MaxAge,
		//	Expires: v.Expires,
		//}
		request.AddCookie(v)
	}
	response, e = client.Do(request)
	s,_ :=ioutil.ReadAll(response.Body)
	defer response.Body.Close()
	return s
}
